//
//  MHDetailLocationViewController.m
//  MiaoCircle
//
//  Created by 许正荣 on 2018/10/24.
//  Copyright © 2018年 acgera. All rights reserved.
//
#import "MHDetailLocationViewController.h"
#import "MHDetailLocationViewModel.h"
#import "MHDetailLocationBottom.h"
#import "MHMapSliderVIew.h"
@interface MHDetailLocationViewController ()<MAMapViewDelegate>
@property (nonatomic, strong) MAMapView *mapView;
@property(nonatomic, strong)  MHDetailLocationViewModel *viewModel;

@property(nonatomic, strong) MHMapSliderVIew *sliderView;

@property(nonatomic, strong) MHDetailLocationBottom *bottom;


@property(nonatomic, strong)  MACircle *circle;

//@property(nonatomic, strong) UISlider *sliderView;

@end

@implementation MHDetailLocationViewController
@dynamic viewModel;

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createMapView];
    self.mapView.centerCoordinate  = CLLocationCoordinate2DMake(self.viewModel.location.lat, self.viewModel.location.lon);
    MAPointAnnotation *poiAnnotation = [[MAPointAnnotation alloc] init];
    poiAnnotation.coordinate = self.mapView.centerCoordinate;
    [self.mapView addAnnotation:poiAnnotation];
    [self.view addSubview:self.bottom];
    self.bottom.addressLabel.text = self.viewModel.location.address;
    self.bottom.areaNameLabel.text = self.viewModel.location.areaName;
    YYCache *cache=[YYCache cacheWithName:MHCacheGlobleKey];
     MHRedEnvelopeConfig *config  =  [cache objectForKey:MHRedPacketConfig];
    self.sliderView.disatantSlider.maximumValue = (config!=nil) ? config.maxRangeKm : 50;
//    self
    [self.bottom mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view);
        make.height.equalTo(@60);
        make.left.right.equalTo(self.view);
    }];
    
    if (self.viewModel.showSelet) {
        MACircle *circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(self.viewModel.location.lat, self.viewModel.location.lon) radius:1000];
        self.circle = circle;
        [_mapView addOverlay: circle];
        
        [self.view addSubview:self.sliderView];
        [self.sliderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.bottom.mas_top);
            make.left.right.equalTo(self.view);
            make.height.equalTo(@143);
        }];
    }else
    {
        self.bottom.backgroundColor = MHWhiteTextColor;
    }
   
}

-(void)rightButtonClick
{
    [self.viewModel.compoleteSubject sendNext:@(self.sliderView.disatantSlider.value)];
    [self.viewModel.services popViewModelAnimated:YES];
}

-(MHMapSliderVIew *)sliderView
{
    if (!_sliderView) {
        _sliderView = [MHMapSliderVIew mh_viewFromXib];
        [_sliderView.disatantSlider addTarget:self action:@selector(sliderChages:) forControlEvents:UIControlEventValueChanged];
         [_sliderView.disatantSlider addTarget:self action:@selector(sliderTouchUpInSide:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _sliderView;
}

- (void) sliderChages:(id)sender {
    UISlider *slider = sender;
    NSUInteger index = (NSUInteger)(slider.value + 0.5);
    [self.sliderView.disatantSlider setValue:index animated:YES];
    [self.sliderView.distantBtn setTitle:[NSString stringWithFormat:@"%lukm",(unsigned long)index] forState:0];
    UIImage *image =[UIImage imageNamed:@"ic_map_rulerred"];
    image = [image cropImageToRect:CGRectMake(0, 0, image.size.width * slider.value/slider.maximumValue, image.size.height)];
    
    [self.sliderView.disatantSlider setMinimumTrackImage:image  forState:UIControlStateNormal];
    

}

-(void) sliderTouchUpInSide :(id)sender
{
    UISlider *slider = sender;
    NSUInteger index = (NSUInteger)(slider.value + 0.5);
    MACircle *circle = [MACircle circleWithCenterCoordinate:CLLocationCoordinate2DMake(self.viewModel.location.lat, self.viewModel.location.lon) radius:index * 1000.0];
    _mapView.zoomLevel = [self getZoomLevealByDistant:index];
    [_mapView addOverlay: circle];
    [_mapView removeOverlay:self.circle];
    self.circle = circle;
}


-(CGFloat ) getZoomLevealByDistant:(int )Distant
{
    
//    20-10m-（19=<zoom<20）
//    19-10m-（19=<zoom<20）
//    18-25m-（18=<zoom<19）
//    17-50m-（17=<zoom<18）
//    16-100m-（16=<zoom<17）
//    15-200m-（15=<zoom<16）
//    14-500m-（14=<zoom<15）
//    13-1km-（13=<zoom<14）
//    12-2km-（12=<zoom<13）
//    11-5km-（11=<zoom<12）
//    10-10km-（10=<zoom<11）
//    9-20km-（9=<zoom<10）
//    8-30km-（8=<zoom<9）
//    7-50km-（7=<zoom<8）
//    6-100km-（6=<zoom<7）
//    5-200km-（5=<zoom<6）
//    4-500km-（4=<zoom<5）
//    3-1000km-（3=<zoom<4）
//    2-1000km-（3=<zoom<4）

        CGFloat zoomLevel = 0;
        switch (Distant) {
            case 1:
               zoomLevel = 14;
                break;
            case 2:
                zoomLevel = 13;
                break;
            case 3:
                zoomLevel = (3/4.0 )+ 12;
                break;
            case 4:
                zoomLevel = 12;
                break;
            case 5:
                zoomLevel = 11.5;
                break;
            case 6:case 7:case 8: case 9:
                zoomLevel = 10 + ( Distant*2.0/20);
                break;
            case 10:
                zoomLevel = 10;
            case 11:case 12:case 13:case 14:case 15:
                zoomLevel = 9 + (Distant * 2.0/40);
                break;
            default:
                zoomLevel =  8;
                break;
        }
    return zoomLevel;
}

-(MHDetailLocationBottom *)bottom
{
    if (!_bottom) {
        _bottom = [MHDetailLocationBottom mh_viewFromXib];
        
    }
    return _bottom;
}


-(void) createMapView
{
    
    self.mapView = [[MAMapView alloc] initWithFrame:self.view.bounds];
    self.mapView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    self.mapView.zoomLevel = self.viewModel.showSelet ? 14 :  14.5;
    [self.mapView setMaxZoomLevel:18];
    self.mapView.showsCompass = NO;
    self.mapView.delegate = self;
    [self.view addSubview:self.mapView];
}


- (MAOverlayRenderer *)mapView:(MAMapView *)mapView rendererForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MACircle class]])
    {
        MACircleRenderer *circleRenderer = [[MACircleRenderer alloc] initWithCircle:overlay];
        circleRenderer.fillColor    = [MHColorFromHexString(@"#ADDDFC") colorWithAlphaComponent:0.5];
        return circleRenderer;
    }
    return nil;
}

- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *busStopIdentifier = @"districtIdentifier";
        MAPinAnnotationView *poiAnnotationView = (MAPinAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:busStopIdentifier];
        if (poiAnnotationView == nil)
        {
            poiAnnotationView = [[MAPinAnnotationView alloc] initWithAnnotation:annotation
                                                                reuseIdentifier:busStopIdentifier];
        }
        poiAnnotationView.image = [UIImage imageNamed:@"icon_cricle_maplocation"];
        poiAnnotationView.canShowCallout = NO;
        return poiAnnotationView;
    }
    return nil;
}


@end
